﻿using Newtonsoft.Json;
using RTW.OA.Areas.Common.Servers;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.Common.Controllers
{
    public class DinnerController : Controller
    {
        private CookBookServers cookbookServers;
        private OrderServers orderServers;
        public DinnerController()
        {
            cookbookServers = new CookBookServers();
            orderServers = new OrderServers();
        }

        // GET: Common/Dinner
        public ActionResult CookBook()
        {
            ViewBag.FY_IDData = JsonConvert.SerializeObject(cookbookServers.GetFY_ID());
            return View();
        }

        // GET: Common/CookBook/Edit/5
        public ActionResult CookBookEdit(int? id)
        {
            ViewBag.FY_IDData = cookbookServers.GetFY_ID();
            var model = new dinner_cookbook();
            if (id != null)
            {
                ViewBag.Title = "编辑菜单信息表";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建菜单信息表";
            }
            return View(model);
        }

        // POST: Common/CookBook/Edit/5
        [HttpPost]
        public ActionResult CookBookEdit(int? id, dinner_cookbook model)
        {
            ViewBag.FY_IDData = cookbookServers.GetFY_ID();
            try
            {
                if (id == null || id == 0)
                {
                    cookbookServers.Add(model);
                    ViewBag.msg = "新增菜单信息表信息成功！";
                    model = new dinner_cookbook();
                }
                else
                {
                    cookbookServers.Update(model);
                    ViewBag.msg = "修改菜单信息表信息成功！";
                }
            }
            catch 
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: Common/CookBook/Delete/5
        public ActionResult CookBookDelete(int id)
        {
            if (id != 0)
            {
                cookbookServers.Del(id.ToString());
                ViewBag.msg = "删除菜单信息表信息成功！";
            }
            return Content("");
        }

        // GET: Common/Order
        public ActionResult Order()
        {
            ViewBag.FY_IDData = JsonConvert.SerializeObject(orderServers.GetFY_ID());
            ViewBag.WHICH_MEALData = JsonConvert.SerializeObject(orderServers.GetWHICH_MEAL());
            ViewBag.MENU_IDData = JsonConvert.SerializeObject(orderServers.GetMENU_ID());
            return View();
        }

        // GET: Common/Order/Edit/5
        public ActionResult OrderEdit(int? id)
        {
            ViewBag.FY_IDData = orderServers.GetFY_ID();
            ViewBag.WHICH_MEALData = orderServers.GetWHICH_MEAL();
            ViewBag.MENU_IDData = orderServers.GetMENU_ID();
            var model = new dinner_order();
            if (id != null)
            {
                ViewBag.Title = "编辑订餐表";
                model = GetOrderModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建订餐表";
            }
            return View(model);
        }

        // POST: Common/Order/Edit/5
        [HttpPost]
        public ActionResult OrderEdit(int? id, dinner_order model)
        {
            ViewBag.FY_IDData = orderServers.GetFY_ID();
            ViewBag.WHICH_MEALData = orderServers.GetWHICH_MEAL();
            ViewBag.MENU_IDData = orderServers.GetMENU_ID();
            try
            {
                if (id == null || id == 0)
                {
                    orderServers.Add(model);
                    ViewBag.msg = "新增订餐表信息成功！";
                    model = new dinner_order();

                }
                else
                {
                    orderServers.Update(model);
                    ViewBag.msg = "修改订餐表信息成功！";

                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: Common/Order/Delete/5
        public ActionResult OrderDelete(int id)
        {
            if (id != 0)
            {
                orderServers.Del(id.ToString());
                ViewBag.msg = "删除订餐表信息成功！";
            }
            return Content("");
        }

        #region //GetData
        public string GetTableJsonResult()
        {
            var list = cookbookServers.GetData();
            return JsonConvert.SerializeObject(list);
        }

        public dinner_cookbook GetModel(string id)
        {
            var model = cookbookServers.GetModelById(id);
            return model;
        }

        public string GetOrderTableJsonResult( )
        {
            var list = orderServers.GetData();
            return JsonConvert.SerializeObject(list);
        }

        public dinner_order GetOrderModel(string id)
        {
            var model = orderServers.GetModelById(id);
            return model;
        }
        #endregion //GetData
    }
}
